package catchData;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

/**
 * @author suwy
 * @date 2014-2-14
 * @todo TODO
 */
public class CatchNationalData4 {
	private static final Logger LOG = Logger.getLogger(CatchNationalData4.class);
	private static String secondURL = "http://data.stats.gov.cn/quotas/getchildren?dbcode=hgnd&dimension=zb";
	private static String level3 = "&level=3&code=";
	
	private void excute() {
		HttpClient client = CatchDataUtil.initHttpClient();
		GetMethod secondGet = null;
		String[] codes = {"A0I0201",
				"A0I0202",
				"A0I0203",
				"A0I0204",
				"A0I0205",
				"A0I0206",
				"A0I0207",
				"A0I0208",
				"A0I0209",
				"A0I020A",
				"A0I020B",
				"A0I020C",
				"A0I020D",
				"A0I020E",
				"A0I0301",
				"A0I0302",
				"A0I0303",
				"A0I0304",
				"A0I0305",
				"A0I0306",
				"A0I0307",
				"A0I0308",
				"A0I0309",
				"A0I030A",
				"A0I0401",
				"A0I0402",
				"A0I0403",
				"A0I0404",
				"A0I0405",
				"A0I0406",
				"A0I0407",
				"A0I0408",
				"A0I0501",
				"A0I0502",
				"A0I0503",
				"A0I0504",
				"A0I0505",
				"A0I0506",
				"A0I0507",
				"A0I0508",
				"A0I0509",
				"A0I050A",
				"A0I050B",
				"A0I050C",
				"A0I050D",
				"A0I050E",
				"A0I0601",
				"A0I0602",
				"A0I0603",
				"A0I0604",
				"A0I0605",
				"A0I0606",
				"A0I0607",
				"A0I0608",
				"A0I0609",
				"A0I060A",
				"A0I0701",
				"A0I0702",
				"A0I0703",
				"A0I0704",
				"A0I0705",
				"A0I0706",
				"A0I0707",
				"A0I0708",
				"A0I0801",
				"A0I0802",
				"A0I0803",
				"A0I0804",
				"A0I0805",
				"A0I0806",
				"A0I0807",
				"A0I0808",
				"A0I0809",
				"A0I080A",
				"A0I080B",
				"A0I080C",
				"A0I0901",
				"A0I0902",
				"A0I0903",
				"A0I0904",
				"A0I0905",
				"A0I0906",
				"A0I0A01",
				"A0I0A02",
				"A0I0A03",
				"A0I0A04",
				"A0O0801",
				"A0O0802",
				"A0O0803",
				"A0O0804",
				"A0O0805",
				"A0O0806",
				"A0O0807",
				"A0O0808",
				"A0O0809",
				"A0O0E01",
				"A0O0E02",
				"A0O0F01",
				"A0O0F02",
				"A0O0F03",
				"A0O0G01",
				"A0O0G02",
				"A0O0G03",
				"A0O0H01",
				"A0O0H02",
				"A0O0H03",
				"A0S0701",
				"A0S0702",
				"A0S0C01",
				"A0S0C02",
				"A0S0C03",
				"A0S0D01",
				"A0S0D02",
				"A0S0D03",
				"A0S0D04",
				"A0S0E01",
				"A0S0E02",
				"A0S0E03",
				"A0S0E04",
				"A0S0E05",
				"A0S0E06",
				"A0S0F01",
				"A0S0F02",
				"A0S0F03",
				"A0S0F04",
				"A0S0F05",
				"A0S0G01",
				"A0S0G02",
				"A0S0G03",
				"A0S0G04",
				"A0S0H01",
				"A0S0H02",
				"A0S0H03",
				"A0S0I01",
				"A0S0I02",
				"A0S0I03",
				"A0S0I04",
				"A0S0I05",
				"A0S0I06",
				"A0S0I07",
				"A0S0J01",
				"A0S0J02",
				"A0S0J03",
				"A0S0J04",
				"A0S0J05",
				"A0S0J06",
				"A0S0K01",
				"A0S0K02",
				"A0S0K03",
				"A0S0M01",
				"A0S0M02",
				"A0S0O01",
				"A0S0O02",
				"A0S0Q01",
				"A0S0Q02",
				"A0S0Q03",
				"A0S0Q04",
				"A0S0Q05",
				"A0S0Q06",
				"A0S0Q07",
				"A0S0R01",
				"A0S0R02",
				"A0S0R03",
				"A0S0R04",
				"A0S0R05",
				"A0S0R06",
				"A0S0R07",
				"A0S0S01",
				"A0S0S02",
				"A0S0S03",
				"A0S0S04",
				"A0S0S05",
				"A0S0S06",
				"A0S0S07",
				"A0S0T01",
				"A0S0T02",
				"A0S0T03",
				"A0S0T04",
				"A0S0T05",
				"A0S0T06",
				"A0S0T07",
				"A0S0T08"};
		StringBuilder stringBuilder = new StringBuilder();
		for(int i = 0; i < codes.length; i++) {
			secondGet = new GetMethod(secondURL + level3 + codes[i]);
			CatchDataUtil.setHeaders(secondGet, secondURL + level3 + codes[i],
					CatchDataUtil.getWebSite(secondURL + level3 + codes[i]));
//			LOG.info(secondURL + level3 + codes[i]);
			try {
				client.executeMethod(secondGet);
				stringBuilder.append(secondGet.getResponseBodyAsString()
						.substring(1, secondGet.getResponseBodyAsString().length()-1) + ",");
			} catch (HttpException e) {
				LOG.info(e);
			} catch (IOException e) {
				LOG.info(e);
			} finally {
				secondGet.releaseConnection();
			}
		}
		LOG.info(stringBuilder);
	}
	
	public static void main(String[] args) {
		CatchNationalData4 instance = new CatchNationalData4();
		instance.excute();
	}

}
